package com.jic.data.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jic.data.constant.consist.ResponseCode;
import com.jic.data.exception.AppRuntimeException;
import com.jic.data.mapper.CustomerConsumeTrendsStsMapper;
import com.jic.data.model.entity.CustomTrendChat;
import com.jic.data.model.entity.CustomTypeBuyUnitPrice;
import com.jic.data.model.entity.CustomerConsumeTrendsSts;
import com.jic.data.model.vo.request.CustomerConsumeTrendsStsRequest;
import com.jic.data.service.CustomerConsumeTrendsStsService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 接口实现类
 *
 * @author : 建投数据科技(山东)有限公司
 * @version : v1.0.0
 * @email :
 * @since : 2020-03-09 10:12:20
 */
@Service
@Slf4j
public class CustomerConsumeTrendsStsServiceImpl implements CustomerConsumeTrendsStsService {

    @Resource
    private CustomerConsumeTrendsStsMapper mapper;


    /**
     * 新增记录
     *
     * @param request 需要保存的入参实体类
     * @return int 是否保存成功
     */
    @Override
    public int save(CustomerConsumeTrendsSts request) {
        int flag = mapper.insert(request);
        if (flag == 1) {
            return 1;
        } else {
            throw new AppRuntimeException(ResponseCode.EXCEPTION, "新增失败");
        }
    }


    /**
     * 保存或修改记录信息
     *
     * @param request 需要保存的入参实体类
     * @return int 是否保存或修改成功
     * author 建投数据科技(山东)有限公司
     * @date 2020-3-11 20:54:09
     */
    @Override
    public int saveOrUpdate(CustomerConsumeTrendsSts request) {
        log.info("保存或修改记录信息，入参 = {}", JSONObject.toJSONString(request));
        try {
            if (request == null) {
                throw new AppRuntimeException(ResponseCode.EXCEPTION, "入参为空");
            }
            if (request.getId() != null && request.getId() != 0) {
                log.info("保存或修改记录信息，修改，入参 = {}", JSONObject.toJSONString(request));
                int flag = mapper.updateByPrimaryKey(request);
                if (flag == 1) {
                    return 1;
                }
            } else {
                log.info("保存或修改记录信息，保存，入参 = {}", JSONObject.toJSONString(request));
                int flag = mapper.insertSelective(request);
                if (flag == 1) {
                    return 1;
                } else {
                    throw new AppRuntimeException(ResponseCode.EXCEPTION, "修改失败");
                }
            }
        } catch (Exception e) {
            log.error("用户保存或修改失败", e);
            throw new AppRuntimeException(ResponseCode.EXCEPTION, "用户保存或修改失败");
        }
        return -1;
    }

    /**
     * 删除记录
     *
     * @param request 需要删除,含有主键的入参实体类
     * @return int 是否删除成功
     */
    @Override
    public int deleteByPrimaryKey(CustomerConsumeTrendsSts request) {
        int flag = mapper.deleteByPrimaryKey(request);
        if (flag == 1) {
            return 1;
        } else {
            throw new AppRuntimeException(ResponseCode.EXCEPTION, "删除失败");
        }
    }


    /**
     * 逻辑删除记录
     *
     * @param request 含有主键的逻辑删除实体类
     * @return int 是否逻辑删除成功
     */
    @Override
    public int deleteByPrimaryKeyLogically(CustomerConsumeTrendsSts request) {
        request.setDeleteFlag(1);
        int flag = mapper.updateByPrimaryKey(request);
        if (flag == 1) {
            return 1;
        } else {
            throw new AppRuntimeException(ResponseCode.EXCEPTION, "逻辑删除记录失败");
        }
    }


    /**
     * 修改
     *
     * @param request
     * @return
     */
    @Override
    public int updateByPrimaryKeySelective(CustomerConsumeTrendsSts request) {
        try {
            log.info("修改记录");
            CustomerConsumeTrendsSts param = new CustomerConsumeTrendsSts();
            BeanUtils.copyProperties(request, param);
            int flag = mapper.updateByPrimaryKeySelective(param);
            if (flag == 1) {
                return 1;
            } else {
                throw new AppRuntimeException(ResponseCode.EXCEPTION, "修改记录失败");
            }
        } catch (Exception e) {
            log.error("修改记录失败", e);
            throw new AppRuntimeException(ResponseCode.EXCEPTION, "修改记录失败");
        }

    }

    /**
     * 记录详情
     *
     * @param request 查询的入参实体类
     * @return 数据库查询到的实体类
     */
    @Override
    public CustomerConsumeTrendsSts getByPrimaryKey(CustomerConsumeTrendsSts request) {
        return mapper.selectByPrimaryKey(request);
    }


    /**
     * 分页查询
     *
     * @param request 分页查询入参实体类
     * @return 含有分页信息的实体类列表
     */
    @Override
    public PageInfo<CustomerConsumeTrendsSts> listPageBySelective(CustomerConsumeTrendsSts request, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        PageHelper.orderBy(" create_time desc");
        List<CustomerConsumeTrendsSts> list = mapper.listPageBySelective(request);
        return new PageInfo(list);
    }

    /**
     * 不分页查询
     *
     * @param request 不分页查询入参实体类
     * @return 数据库查到的实体类列表
     */
    @Override
    public List<CustomerConsumeTrendsSts> listAllRecord(CustomerConsumeTrendsSts request) {
        //分页处理
        request.setDeleteFlag(0);
        return mapper.selectBySelective(request);
    }

    @Override
    public Map secCustomTypeBuyUnitPrice(CustomerConsumeTrendsStsRequest request) {
        HashMap<String, List> result = new HashMap<>();
        ArrayList<String> datas = new ArrayList<>();
        ArrayList<Long> one = new ArrayList<>();
        ArrayList<Long> two = new ArrayList<>();
        request.setDeleteFlag(0);
        if (request.getChannelId() != null) {
            request.setStsType(3);
            request.setPlatformId(null);
            request.setStoreId(null);
            request.setStoreName(null);
        } else if (request.getPlatformId() != null) {
            request.setStsType(2);
            request.setStoreId(null);
            request.setStoreName(null);
        } else if (request.getStoreId() != null) {
            request.setStsType(1);
            request.setStoreName(null);
        } else if (StringUtils.isNotEmpty(request.getStoreName())) {
            request.setStsType(1);
        } else {
            request.setStsType(4);
        }
        List<CustomerConsumeTrendsSts> list = mapper.selectAll(request);
        if (CollectionUtil.isNotEmpty(list)) {
            CustomerConsumeTrendsSts sts = list.get(0);
            String s = sts.getPeriodStsData();
            if (StringUtils.isNotEmpty(s)) {
                List<CustomTypeBuyUnitPrice> array = JSONObject.parseArray(s, CustomTypeBuyUnitPrice.class);
                if (CollectionUtil.isNotEmpty(array)) {
                    for (CustomTypeBuyUnitPrice price : array) {
                        datas.add(price.getConsumeDate());
                        one.add(price.getOneConsumeAvgAmount());
                        two.add(price.getMoreConsumeAvgAmount());
                    }
                }
            }
        }
        result.put("data", datas);
        result.put("one", one);
        result.put("two", two);
        return result;
    }

    @Override
    public Map secCustomTrend(CustomerConsumeTrendsStsRequest request) {
        HashMap<String, List> result = new HashMap<>();
        ArrayList<String> datas = new ArrayList<>();
        ArrayList<String> time = new ArrayList<>();
        ArrayList<Double> saleNumber = new ArrayList<>();
        ArrayList<Long> orderNumber = new ArrayList<>();
        request.setDeleteFlag(0);
        if (request.getChannelId() != null) {
            request.setStsType(3);
            request.setPlatformId(null);
            request.setStoreId(null);
            request.setStoreName(null);
        } else if (request.getPlatformId() != null) {
            request.setStsType(2);
            request.setStoreId(null);
            request.setStoreName(null);
        } else if (request.getStoreId() != null) {
            request.setStsType(1);
            request.setStoreName(null);
        } else if (StringUtils.isNotEmpty(request.getStoreName())) {
            request.setStsType(1);
        } else {
            request.setStsType(4);
        }
        List<CustomerConsumeTrendsSts> list = mapper.secCustomTrend(request);
        if (CollectionUtil.isNotEmpty(list)) {
            CustomerConsumeTrendsSts sts = list.get(0);
            String s = sts.getPeriodStsData();
            if (StringUtils.isNotEmpty(s)) {
                List<CustomTrendChat> array = JSONObject.parseArray(s, CustomTrendChat.class);
                if (CollectionUtil.isNotEmpty(array)) {
                    for (CustomTrendChat trend : array) {
                        time.add(trend.getConsumeDate());
                        saleNumber.add(trend.getConsumeAmount());
                        orderNumber.add(trend.getOrderNumber());
                    }
                }
            }
        }
        result.put("time", time);
        result.put("saleNumber", saleNumber);
        result.put("orderNumber", orderNumber);
        return result;
    }


}
